<template>
    <section class="newsMain">
      <div class="topTitle blue getTop">
        消息中心
      </div>
      <div class="list">
        <div v-if="dataList.length<1" class="notHave">没有TA的消息</div>
        <ul>
          <li @click="goIm(item.user_id)" v-for="item in dataList">
            <img v-if="item.avatar!=''" :src="item.avatar">
            <img v-else :src="$store.state.defLogo">
            <span v-if="item.message_count!=0" class="number">{{item.message_count|moreNum}}</span>
            <span class="time">{{item.send_message_timestamp|filterTime}}</span>
            <p v-if="item.nickname!=''" class="name">{{item.nickname}}</p>
            <p v-else class="name">{{item.user_id}}</p>
            <p class="title">{{item.last_message.message|messageFilter}}</p>
          </li>
        </ul>
      </div>
    </section>
</template>

<script>
import axios from 'axios'
import {Post,Get} from '@/config/api.js'

export default {
  name: 'hello',
  data() {
    return {
      kefuData:'',//客服信息
      dataList:''
    }
  },
  watch:{
    selected:function(){
      this.active=this.selected
    }
  },
  filters:{
    moreNum(val){
      if(val>9){
        return '..'
      }else{
        return val
      }
    },
    messageFilter(val){
      let len=val.length
      for(let i=0;i<len;i++){
        if(val.indexOf("[):]")>-1){
          val = val.replace("[):]",'[表情]');
        }
        if(val.indexOf("[:D]")>-1){
          val = val.replace("[:D]",'[表情]');
        }
        if(val.indexOf("[;)]")>-1){
          val = val.replace("[;)]",'[表情]');
        }
        if(val.indexOf("[:-o]")>-1){
          val = val.replace("[:-o]",'[表情]');
        }
        if(val.indexOf("[:p]")>-1){
          val = val.replace("[:p]",'[表情]');
        }
        if(val.indexOf("[(H)]")>-1){
          val = val.replace("[(H)]",'[表情]');
        }
        if(val.indexOf("[:@]")>-1){
          val = val.replace("[:@]",'[表情]');
        }
        if(val.indexOf("[:s]")>-1){
          val = val.replace("[:s]",'[表情]');
        }
        if(val.indexOf("[:$]")>-1){
          val = val.replace("[:$]",'[表情]');
        }
        if(val.indexOf("[:(]")>-1){
          val = val.replace("[:(]",'[表情]');
        }

        if(val.indexOf("[:\'(]")>-1){
          val = val.replace("[:\'(]",'[表情]');
        }
        if(val.indexOf("[:|]")>-1){
          val = val.replace("[:|]",'[表情]');
        }
        if(val.indexOf("[(a)]")>-1){
          val = val.replace("[(a)]",'[表情]');
        }
        if(val.indexOf("[8o|]")>-1){
          val = val.replace("[8o|]",'[表情]');
        }
        if(val.indexOf("[8-|]")>-1){
          val = val.replace("[8-|]",'[表情]');
        }
        if(val.indexOf("[+o(]")>-1){
          val = val.replace("[+o(]",'[表情]');
        }
        if(val.indexOf("[<o)]")>-1){
          val = val.replace("[<o)]",'[表情]');
        }
        if(val.indexOf("[|-)]")>-1){
          val = val.replace("[|-)]",'[表情]');
        }
        if(val.indexOf("[*-)]")>-1){
          val = val.replace("[*-)]",'[表情]');
        }
        if(val.indexOf("[:-#]")>-1){
          val = val.replace("[:-#]",'[表情]');
        }

        if(val.indexOf("[:-*]")>-1){
          val = val.replace("[:-*]",'[表情]');
        }
        if(val.indexOf("[^o)]")>-1){
          val = val.replace("[^o)]",'[表情]');
        }
        if(val.indexOf("[8-)]")>-1){
          val = val.replace("[8-)]",'[表情]');
        }
        if(val.indexOf("[(|)]")>-1){
          val = val.replace("[(|)]",'[表情]');
        }
        if(val.indexOf("[(u)]")>-1){
          val = val.replace("[(u)]",'[表情]');
        }
        if(val.indexOf("[(S)]")>-1){
          val = val.replace("[(S)]",'[表情]');
        }
        if(val.indexOf("[(*)]")>-1){
          val = val.replace("[(*)]",'[表情]');
        }
        if(val.indexOf("[(#)]")>-1){
          val = val.replace("[(#)]",'[表情]');
        }
        if(val.indexOf("[(R)]")>-1){
          val = val.replace("[(R)]",'[表情]');
        }
        if(val.indexOf("[({)]")>-1){
          val = val.replace("[({)]",'[表情]');
        }
      }
      return val
    }
  },
  methods:{
    //返回上一页
    goBack(){
      this.$router.go(-1)
    },
    //去聊天
    goIm(val){
      this.$router.push({ path: '/webim1', query: { uid: val }})
    },
    //信息列表
    async getData(){
      await Get('/chat/message/list?_sort=send_message_timestamp',this.$store.state.token).then((res)=>{
        this.dataList=res.data.reverse()
      })
    },
    loadMore() {
      this.loading = true;
      setTimeout(() => {
        let last = this.list[this.list.length - 1];
        for (let i = 1; i <= 10; i++) {
          this.list.push(last + i);
        }
        this.loading = false;
      }, 2500);
    }
  },
  created: function () {
  },
  mounted: function () {
    this.getData()
    var wh = $(window).height();
    $('.newsMain').css('min-height',wh)
  },
  activated: function () {
    console.log(3)
  },
  deactivated: function () {
    console.log(4)
  }
}

</script>
